import json
from fastapi import Depends, APIRouter
from pydantic import BaseModel

from routes.slot_management import sql_polars,sqlpyodbc
from util.JWT import get_current_user


outbound_slot = APIRouter()

# 刷新


@outbound_slot.get("/getoutbound")
async def getoutbound(id: dict = Depends(get_current_user)):
    sql="SELECT ConCaseNo,ContractNo,CaseNo,Size,CasingType,PackageType_C,LocationID FROM CaseResult WHERE PackageQtyStatus=1 AND LocationID !='' AND isout=0"
    df = sql_polars.query(sql)
    if df.height < 1:
        return {
            "code": 204,
            "status": "no",
            "msg": "没有内容",
            "data": []
        }
    df = df.to_dicts()
    json_string = json.dumps(df, ensure_ascii=False)
    return {
        "code": 200,
        "status": "ok",
        "msg": "success",
        "data": json_string
    }

# 出库
class outboundType(BaseModel):
    ConCaseNo: str

@outbound_slot.post("/outbound")
async def outbound(data: outboundType, id: dict = Depends(get_current_user)):
    sql=f"UPDATE CaseResult SET isout=1 WHERE ConCaseNo='{data.ConCaseNo}'"
    try:
        if sqlpyodbc.execute(sql)>0:
            return {"code": 200, "status": "ok", "msg": "success"}
        return {"code": 400, "status": "no", "msg": "error"}
    except:
        return {"code": 400, "status": "no", "msg": "error"}




